package com.isoftstone.util;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;





/**
 *
 * JsonUtil
 *	json 转换
 *
 * @version 1.0,2015-3-27
 * @author guoX
 *
 */
public class JsonUtil {

	private static Logger logger = Logger.getLogger(JsonUtil.class);

	private static final ObjectMapper MAPPER = new ObjectMapper();

	static {
		MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, false);
	}

	private static final JsonFactory JSONFACTORY = new JsonFactory();

	/**
	 * 转换Java Bean 为 json
	 */
	public static String beanToJson(Object o) {
		StringWriter sw = new StringWriter(300);
		JsonGenerator jsonGenerator = null;

		try {
			jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
			MAPPER.writeValue(jsonGenerator, o);
			return sw.toString();

		} catch (Exception e) {
			logger.error("json转换错误：",e);
			return null;

		} finally {
			if (jsonGenerator != null) {
				try {
					jsonGenerator.close();
				} catch (Exception e) {
					logger.error("json转换错误：",e);
				}
			}
		}
	}

	/**
	 * json 转 javabean
	 * 对象里需要无参构造器
	 * @param json
	 * @return
	 */
	public static Object jsonToBean(String json, Class clazz) {
		try {
			//MAPPER.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
			return MAPPER.readValue(json, clazz);
		} catch (Exception e) {
			logger.error("json转换错误：",e);
			return null;
		}
	}

	/**
	 * 转换Java Bean 为 HashMap
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> beanToMap(Object o) {
		try {
			return MAPPER.readValue(beanToJson(o), HashMap.class);
		} catch (Exception e) {
			logger.error("json转换错误：",e);
			return null;
		}
	}

	/**
	 * 转换Json String 为 HashMap
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> jsonToMap(String json,
												boolean collToString) {
		try {
			Map<String, Object> map = MAPPER.readValue(json, HashMap.class);
			if (collToString) {
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					if (entry.getValue() instanceof Collection
							|| entry.getValue() instanceof Map) {
						entry.setValue(beanToJson(entry.getValue()));
					}
				}
			}
			return map;
		} catch (Exception e) {
			logger.error("json转换错误：",e);
			return null;
		}
	}

	@SuppressWarnings("serial")
	public static class jsonParseException extends Exception {
		public jsonParseException(String message) {
			super(message);
		}
	}

	/**
	 * List 转换成json
	 *
	 * @param list
	 * @return
	 */
	public static String listToJson(List<Map<String, String>> list) {
		JsonGenerator jsonGenerator = null;
		StringWriter sw = new StringWriter();
		try {
			jsonGenerator = JSONFACTORY.createJsonGenerator(sw);
			new ObjectMapper().writeValue(jsonGenerator, list);
			jsonGenerator.flush();
			return sw.toString();
		} catch (Exception e) {
			logger.error("json转换错误：",e);
			return null;
		} finally {
			if (jsonGenerator != null) {
				try {
					jsonGenerator.flush();
					jsonGenerator.close();
				} catch (Exception e) {
					logger.error("json转换错误：",e);
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * List 转换成json
	 *
	 * @param list
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String writeListJSON(List list) {
		String sw = new String();
		try {
			sw = MAPPER.writeValueAsString(list);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			logger.error("json转换错误：",e);
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			logger.error("json转换错误：",e);
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error("json转换错误：",e);
			e.printStackTrace();
		}
		return sw;


	}

	/**
	 * json 转List
	 *
	 * @param json
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, String>> jsonToList(String json) {
		try {
			if (json != null && !"".equals(json.trim())) {
				// JsonParser jsonParse = JSONFACTORY.createJsonParser(new
				// StringReader(json));

				List<Map<String, String>> arrayList = MAPPER.readValue(json,
						ArrayList.class);
				return arrayList;
			} else {
				return null;
			}
		} catch (Exception e) {
			logger.error("json转换错误：",e);
			return null;
		}
	}

}

